<?php
class Anchor extends Eloquent
{
    public static $timestamps = false;

    public function ward()
    {
        return $this->belongs_to('Ward');
    }

    public function houses()
    {
        return $this->has_many('House');
    }

    public function get_address()
    {
        if ($this->id > -1) {
            $st = $this->anchor_address . ', ' .
                $this->ward->ward_name . ', ' .
                $this->ward->district->district_name . ', ' .
                $this->ward->district->city->city_name;
        } else {
            $st = 'Không xác định';
        }

        return $st;
    }

    public function get_name()
    {
        if ($this->id > -1) {
            return $this->get_attribute('name');
        } else {
            return 'Không xác định';
        }
    }

    /**
     * Trả về danh sách điểm mốc theo Phường
     * @param $ward_id
     * @return mixed
     */
    public static function all_by_ward($ward_id)
    {
        return static::where('id', '>', -1)->where_ward_id($ward_id)->get();
    }

    public static function all_by_district($district_id)
    {
        $wards = Ward::where_district_id($district_id)->get();
        $anchors = array();

        foreach ($wards as $w) {
            $anchors = array_merge($anchors, Anchor::all_by_ward($w->id));
        }

        return $anchors;
    }
}